package MessageParser

import (
	"fmt"
	// "bytes"
	"encoding/json"
)

type MessageRequest struct {
	ID   int
	Type int // 0 - Add, 1 - delete, 2 - update, 3 - query
	Name string
	IP   string
	Port int
}

type MessageResponse struct {
	ID   int
	Type int
	Name string
	IP   string
	Port int

	ErrorCode int
}

// MessageRequest Method
func SerializationOfMessageRequest(str string) (MessageRequest, error) {
	var aRequest MessageRequest

	err := json.Unmarshal([]byte(str), &aRequest)

	return aRequest, err
}

func (req MessageRequest) Deserialization() (string, error) {
	b, err := json.Marshal(req)
	return string(b), err
}

func (req MessageRequest) String() string {
	return fmt.Sprintf("ID: %d \nType: %d\nName: %s\nIP: %s\nPort: %d", req.ID, req.Type, req.Name, req.IP, req.Port)
}

// MessageResponse Method
func SerializationOfMessageResponse(str string) (MessageResponse, error) {
	var aResponse MessageResponse

	err := json.Unmarshal([]byte(str), &aResponse)

	return aResponse, err
}

func (res MessageResponse) Deserialization() (string, error) {
	b, err := json.Marshal(res)
	return string(b), err
}

func (res MessageResponse) String() string {
	return fmt.Sprintf("ID: %d \nType: %d\nName: %s\nIP: %s\nPort: %d\nErrorCode: %d", res.ID, res.Type, res.Name, res.IP, res.Port, res.ErrorCode)
}
